﻿using MB.Orm.DB;
using MB.RuleBase.Common;
using Model;
using Public.DataAccess;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business.Service
{

    public class BGoodsCellNo
    {
        /// <summary>
        /// 查询产品信息
        /// </summary>
        /// <param name="seedType"></param>
        /// <returns></returns>
        public List<Goods> GetGoodsInfo(out int total, params object[] parValues)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<Goods> goods = PagedDatabaseExcuteByXmlHelper.NewInstance.GetPagedObjectsByXml3<Goods>("GoodsCellNo", "GetGoodsInfo", 1, 1000, out  total, parValues);
                return goods;
            }
        }

        /// <summary>
        /// 查询产品信息
        /// </summary>
        /// <param name="seedType"></param>
        /// <returns></returns>
        public List<Goods> GetGoodsDetail(string goodscode)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<Goods> goods = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<Goods>("GoodsCellNo", "GetGoodsDetail", goodscode);
                return goods;
            }
        }
        /// <summary>
        /// 修改商品自定义库位
        /// </summary>
        /// <param name="lmSeed"></param>
        /// <returns></returns>
        public bool UpdateGoodsCellNoBatch(List<Goods> goods)
        {
            bool result = false;
            //定义事物
            var oracle = new MB.RuleBase.BulkCopy.SimulatedOracleHelper();
            var db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase();
            var cn = oracle.CreateOracleConnection(db.ConnectionString);
            DbTransaction tran = null;
            try
            {
                // 打开数据连接事物
                cn.Open();
                tran = cn.BeginTransaction();
                if (goods == null || goods.Count <= 0) return false;

                using (MB.RuleBase.BulkCopy.IDbBulkExecute bulk = MB.RuleBase.BulkCopy.DbBulkExecuteFactory.CreateDbBulkExecute(tran))
                {
                    bulk.WriteToServer("GoodsCellNo", "UpdateGoodsCellNoBatch", goods);
                    result = true;
                }
                if (result)
                    tran.Commit();
                else
                    tran.Rollback();
                return result;
            }
            catch (Exception ex)
            {
                tran.Rollback();
                MB.Util.TraceEx.Write(ex.Message);
                return false;
            }
            finally
            {
                if (tran != null) tran.Dispose();
                if (cn != null) cn.Close();
                if (cn != null) cn.Dispose();
            }
        }



        public int UpdateGoodsCellNo(Goods goodsmove)
        {
            return DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQueryByEntity<Goods>("GoodsCellNo", "UpdateGoodsCellNo", goodsmove);
        }

        /// <summary>
        /// 修改商品在报表中是否展示
        /// </summary>
        /// <param name="lmSeed"></param>
        /// <returns></returns>
        public bool UpdateGoodsIsShow(List<Goods> goods)
        {
            bool result = false;
            //定义事物
            var oracle = new MB.RuleBase.BulkCopy.SimulatedOracleHelper();
            var db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase();
            var cn = oracle.CreateOracleConnection(db.ConnectionString);
            DbTransaction tran = null;
            try
            {
                // 打开数据连接事物
                cn.Open();
                tran = cn.BeginTransaction();
                if (goods == null || goods.Count <= 0) return false;

                using (MB.RuleBase.BulkCopy.IDbBulkExecute bulk = MB.RuleBase.BulkCopy.DbBulkExecuteFactory.CreateDbBulkExecute(tran))
                {
                    bulk.WriteToServer("GoodsCellNo", "UpdateGoodsIsShow", goods);
                    result = true;
                }
                if (result)
                    tran.Commit();
                else
                    tran.Rollback();
                return result;
            }
            catch (Exception ex)
            {
                tran.Rollback();
                MB.Util.TraceEx.Write(ex.Message);
                return false;
            }
            finally
            {
                if (tran != null) tran.Dispose();
                if (cn != null) cn.Close();
                if (cn != null) cn.Dispose();
            }
        }


    }
}
